package com.syni.mdd.sjb.community.base.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syni.dto.mddWechatOa.apiCropBusiness.GetEnterpriseCustomersDto;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.WxCorpBusinessRelation;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.WxExternalContact;
import com.syni.mdd.sjb.common.component.utils.common.spring.ApplicationContextHolder;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.RpcResponseVo;
import com.syni.mdd.sjb.common.component.utils.common.utils.StringUtils;
import com.syni.mdd.sjb.common.component.utils.feign.service.mddWechatOa.CropBusinessRestService;
import com.syni.mdd.sjb.community.base.constants.Constants;
import com.syni.mdd.sjb.community.base.constants.ResultCode;
import com.syni.mdd.sjb.community.base.constants.ResultMsg;
import com.syni.mdd.sjb.community.base.listener.SyncExternalcontactEvent;
import com.syni.mdd.sjb.community.base.service.WxCorpBusinessRelationService;
import com.syni.mdd.sjb.community.base.service.WxExternalContactService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/apiCropBusiness")
@Log4j2
public class CropBusinessApi implements CropBusinessRestService {

    @Autowired
    WxCorpBusinessRelationService wxCorpBusinessRelationService;

    @Autowired
    WxExternalContactService wxExternalContactService;

    @PostMapping("/getEnterpriseCustomers")
    public RpcResponseVo getEnterpriseCustomers(@RequestBody GetEnterpriseCustomersDto getEnterpriseCustomersDto) {
        Set<Integer> authIds = getEnterpriseCustomersDto.getBusinessAuthIds();
        List<WxCorpBusinessRelation> wxCorpBusinessRelationList = wxCorpBusinessRelationService.findCorpBusinessRelationBy(authIds);
        if(CollectionUtils.isNotEmpty(wxCorpBusinessRelationList)){
            Set<String> cropIds = wxCorpBusinessRelationList.stream().map(WxCorpBusinessRelation::getCorpid).collect(Collectors.toSet());
            if(StringUtils.isBlank(getEnterpriseCustomersDto.getName())) {
                if (cropIds.size() > 0) {
                    cropIds.forEach(ci -> {
                        //wxCorpBusinessRelationService.handleCorpBusinessAuthReleation(ci);
                        ApplicationContextHolder.getApplicationContext().publishEvent(new SyncExternalcontactEvent("", ci));
                        System.out.println("執行getEnterpriseCustomers");
                    });
                }
            }
            Page page = new Page(getEnterpriseCustomersDto.getPageNum(),getEnterpriseCustomersDto.getPageSize());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in(WxExternalContact.CORPID,cropIds);
            if(StringUtils.isNotBlank(getEnterpriseCustomersDto.getName())) {
                queryWrapper.like(WxExternalContact.NAME,getEnterpriseCustomersDto.getName());
            }
            IPage<WxExternalContact> wxExternalContactIPage = wxExternalContactService.page(page,queryWrapper);
            List<WxExternalContact> wxExternalContacts = wxExternalContactIPage.getRecords();
            getEnterpriseCustomersDto.setCustomers(BeanUtils.copyList(wxExternalContacts, GetEnterpriseCustomersDto.Customers.class));
            getEnterpriseCustomersDto.setCount(wxExternalContactIPage.getTotal());
            getEnterpriseCustomersDto.setIsBindEnterpriseWechat(Constants.COMMON_FLAG_TRUE_INT);
            return RpcResponseVo.success(getEnterpriseCustomersDto, ResultMsg.MSG_GET_SUCCESS);
        }
        return RpcResponseVo.result(ResultCode.CODE_ERROR_100, ResultMsg.CODE_ERROR_05);
    }
}
